package com.ims.core.saleconfig.service.impl;

import java.util.List;

import com.ims.common.annotation.RequestParamsSplit;
import com.ims.common.core.domain.entity.SysUser;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ims.core.saleconfig.mapper.TSalMaterialMapper;
import com.ims.core.saleconfig.domain.TSalMaterial;
import com.ims.core.saleconfig.service.ITSalMaterialService;
import org.springframework.util.CollectionUtils;

/**
 * 物料Service业务层处理
 *
 * @author IMS
 * @date 2024-09-19
 */
@Service
public class TSalMaterialServiceImpl implements ITSalMaterialService
{
    @Autowired
    private TSalMaterialMapper tSalMaterialMapper;

    /**
     * 查询物料
     *
     * @param id 物料主键
     * @return 物料
     */
    @Override
    public TSalMaterial selectTSalMaterialById(Long id)
    {
        return tSalMaterialMapper.selectTSalMaterialById(id);
    }

    /**
     * 查询物料列表
     *
     * @param tSalMaterial 物料
     * @return 物料
     */
    @Override
    @RequestParamsSplit
    public List<TSalMaterial> selectTSalMaterialList(TSalMaterial tSalMaterial)
    {
        return tSalMaterialMapper.selectTSalMaterialList(tSalMaterial);
    }

    /**
     * 新增物料
     *
     * @param tSalMaterial 物料
     * @return 结果
     */
    @Override
    public int insertTSalMaterial(TSalMaterial tSalMaterial)
    {
        tSalMaterial.setCreateTime(DateUtils.getNowDate());
        return tSalMaterialMapper.insertTSalMaterial(tSalMaterial);
    }

    /**
     * 修改物料
     *
     * @param tSalMaterial 物料
     * @return 结果
     */
    @Override
    public int updateTSalMaterial(TSalMaterial tSalMaterial)
    {
        //提交人
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 只更新产品信息
        TSalMaterial orgTSalMaterial = tSalMaterialMapper.selectTSalMaterialById(tSalMaterial.getId());
        orgTSalMaterial.setProductId(tSalMaterial.getProductId());
        orgTSalMaterial.setProductName(tSalMaterial.getProductName());
        tSalMaterial.setUpdateBy(user.getUserName());
        tSalMaterial.setUpdateTime(DateUtils.getNowDate());
        return tSalMaterialMapper.updateTSalMaterial(tSalMaterial);
    }

    /**
     * 批量删除物料
     *
     * @param ids 需要删除的物料主键
     * @return 结果
     */
    @Override
    public int deleteTSalMaterialByIds(Long[] ids)
    {
        return tSalMaterialMapper.deleteTSalMaterialByIds(ids);
    }

    /**
     * 删除物料信息
     *
     * @param id 物料主键
     * @return 结果
     */
    @Override
    public int deleteTSalMaterialById(Long id)
    {
        return tSalMaterialMapper.deleteTSalMaterialById(id);
    }

    /**
     *
     * @param tSalMaterialList
     */
    @Override
    public void syncMaterialByErp(List<TSalMaterial> tSalMaterialList) {
        TSalMaterial tSalMaterialCondition = new TSalMaterial();
        for(TSalMaterial tSalMaterial : tSalMaterialList){
            tSalMaterialCondition.setMaterialCode(tSalMaterial.getMaterialCode());
            List<TSalMaterial> orgTSalMaterialList =  tSalMaterialMapper.selectTSalMaterialList(tSalMaterialCondition);
            if(!CollectionUtils.isEmpty(orgTSalMaterialList)){
                // 已存在，更新其他信息
                TSalMaterial orgTSalMaterial = orgTSalMaterialList.get(0);
                orgTSalMaterial.setMaterialName(tSalMaterial.getMaterialName());
                orgTSalMaterial.setMaterialType(tSalMaterial.getMaterialType());
                orgTSalMaterial.setMaterialTypeName(tSalMaterial.getMaterialTypeName());
                orgTSalMaterial.setStatus(tSalMaterial.getStatus());
                orgTSalMaterial.setStatusName(tSalMaterial.getStatusName());
                orgTSalMaterial.setUnit(tSalMaterial.getUnit());
                orgTSalMaterial.setUnitName(tSalMaterial.getUnitName());

                orgTSalMaterial.setUpdateBy("erp");
                orgTSalMaterial.setUpdateTime(DateUtils.getNowDate());
                tSalMaterialMapper.updateTSalMaterial(orgTSalMaterial);
            } else {
                tSalMaterialMapper.insertTSalMaterial(tSalMaterial);
            }
        }
    }
}
